package com.itheima.Servlet01;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContexxt的对象
        ServletContext servletContext = getServletContext();
        String namespace = servletContext.getInitParameter("namespace");
        System.out.println(namespace);
        //获取虚拟目录 、虚拟目录是run里面配置的那个路径
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        //通过虚拟路径获取项目的绝对路径
        String realPath = servletContext.getRealPath(contextPath);
        System.out.println(realPath);
        String realPath1 = servletContext.getRealPath("/b.html");
        System.out.println(realPath1);
        //src下的文件在web-inf/classes里面
        String realPath2 = servletContext.getRealPath("/WEB-INF/classes");
        System.out.println(realPath2);
        servletContext.setAttribute("student","xiaoqiang");
        //servletContext.removeAttribute("student");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
